package com.clean.notification.noti;


import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.ServiceInfo;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.IBinder;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.clean.notification.ShowUtils;
import com.clean.notification.app.BaseApplication;
import com.clean.notification.R;
import com.clean.notification.bean.ResidentNoticeConfig;

import java.lang.reflect.Method;
import java.util.Iterator;

import kotlin.jvm.internal.Intrinsics;

/**
 * 前台通知
 * @author Admin
 */
public class ForegroundNotifyService extends Service {
    static boolean isLiving;
    static boolean isShowing;
    static String CHANNEL_ID1 = "94219";
    static String CHANNEL_NAME1 = "Jjkongoing19299";
    static int Notification_ID1 = 1999922;

    public ForegroundNotifyService() {
    }

    public static boolean getIsShowing() {
        return isShowing;
    }

    public static void startNotifyService(Context context) {
        try {
            Intent intent = new Intent(context, ForegroundNotifyService.class);
            intent.setPackage(context.getPackageName());
            if (VERSION.SDK_INT >= 26) {
                context.startForegroundService(intent);
            } else {
                context.startService(intent);
            }
        } catch (Exception var14) {
            var14.printStackTrace();
        }

    }

    public void onCreate() {
        isLiving = true;
        isShowing = false;
        super.onCreate();
        try {
            if (VERSION.SDK_INT >= 29) {
                this.startForeground(Notification_ID1, GreencreateOngoingNotification(CHANNEL_NAME1), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
            } else {
                this.startForeground(Notification_ID1, GreencreateOngoingNotification(CHANNEL_NAME1));
            }
            isShowing = true;
            if (!ShowUtils.INSTANCE.isNotificationEnabled(this)) {
                isShowing = false;
            }
        } catch (Exception var81) {
            isShowing = false;
        }

    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        isLiving = true;

        try {
            if (VERSION.SDK_INT >= 29) {
                this.startForeground(Notification_ID1, GreencreateOngoingNotification(CHANNEL_NAME1), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
            } else {
                this.startForeground(Notification_ID1, GreencreateOngoingNotification(CHANNEL_NAME1));
            }
            isShowing = true;
            if (!ShowUtils.INSTANCE.isNotificationEnabled(this)) {
                isShowing = false;
            }
        } catch (Exception var97) {
            isShowing = false;
        }
//        if (BuildConfig.DEBUG) {
//            try {
//                GreenCoreMainManager.INSTANCE.getHandler().postDelayed(() -> {
//                    isLiving = this.GreenisServiceRunningInForeground(this, GreenForegroundNotifyService.class);
//                    isShowing = GreenNotifyManagerUtils.hasNotify(Notification_ID1);
//                    Log.e("xxx", "ForegroundNotifyService-->isShowing=$isShowing, isServiceRunningInForeground=$isLiving");
//                }, 1000L);
//            } catch (Exception var96) {
//                var96.printStackTrace();
//            }
//        }
        return 1;
    }


    @Nullable
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onDestroy() {
        isLiving = false;
        isShowing = false;
        super.onDestroy();
    }

    private boolean GreenisServiceRunningInForeground(Context context, Class serviceClass) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        Iterator var49 = manager.getRunningServices(Integer.MAX_VALUE).iterator();
        ActivityManager.RunningServiceInfo runningService;
        do {
            if (!var49.hasNext()) {
                return false;
            }

            runningService = (ActivityManager.RunningServiceInfo) var49.next();
        } while (serviceClass.getName() != runningService.service.getClassName() || !runningService.foreground);

        return true;
    }

    public static Notification GreencreateOngoingNotification(String channelName) {
        NotificationManager manager = (NotificationManager) BaseApplication.mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if (VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID1, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(false);
            channel.enableVibration(false);
            channel.setSound((Uri) null, (AudioAttributes) null);
            channel.setLockscreenVisibility(-1);
            channel.setLightColor(0);
            channel.setVibrationPattern(new long[]{0L});
            manager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(BaseApplication.mContext, CHANNEL_ID1);

        RemoteViews remoteView = ResidentNoticeConfig.GreenOngoingRemoteView();

        RemoteViews remoteViewBig = ResidentNoticeConfig.GreenOngoingRemoteViewBig();

        if (VERSION.SDK_INT >= 31) {
            builder.setCustomContentView(remoteView);
            builder.setContent(remoteView);
            builder.setCustomBigContentView(remoteViewBig);
        } else {
            builder.setContent(remoteViewBig);
            builder.setCustomContentView(remoteViewBig);
            builder.setCustomBigContentView(remoteViewBig);
        }
        Notification notification = builder.setWhen(System.currentTimeMillis()).setOnlyAlertOnce(true).setSmallIcon(R.mipmap.logo).setShowWhen(true).setOngoing(true).setSound((Uri) null).setNumber(6).setVibrate(new long[]{0L}).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setSound((Uri) null, AudioManager.STREAM_NOTIFICATION).setLights(0, 0, 0).build();
        notification.flags = 32;
        return notification;
    }
}
